package com.casems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.casems.commons.dto.CaseData;
import com.casems.commons.dto.CasesDTO;
import com.casems.commons.vo.CasesVO;
import com.casems.entity.Cases;
import com.casems.entity.CasesMedical;
import com.casems.entity.CasesSymptom;
import com.casems.entity.User;
import com.casems.mapper.*;
import com.casems.service.CasesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Case;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tch
 * @since 2023-05-17
 */
@Service
public class CasesServiceImpl extends ServiceImpl<CasesMapper, Cases> implements CasesService {

    @Autowired
    CasesMapper casesMapper;

    @Autowired
    CasesMedicalMapper casesMedicalMapper;

    @Autowired
    CasesSymptomMapper casesSymptomMapper;

    @Autowired
    SymptomMapper symptomMapper;

    @Autowired
    MedicalMapper medicalMapper;

    @Override
    public List<CasesDTO> getByUserId(int userId) {
        return casesMapper.getCasesByUserId(userId);
    }

    @Override
    public List<CasesDTO> getByUser(User user) {
        return casesMapper.getCasesByUser(user);
    }

    @Transactional
    @Override
    public boolean addCases(CasesVO casesVO) {
        casesVO.setCreatetime(new Timestamp(System.currentTimeMillis()));
        int count = casesMapper.insert(casesVO);
        casesVO.getMedicalList()
                .forEach(m -> casesMedicalMapper.insert(new CasesMedical(null,casesVO.getId(),m)));
        casesVO.getSymptomList()
                .forEach(s -> casesSymptomMapper.insert(new CasesSymptom(null,casesVO.getId(),s)));
        return count>0;
    }

    @Transactional
    @Override
    public boolean deleteCases(CasesVO casesVO) {
        int count = 0;
        count+=casesMedicalMapper.deleteBatchIds(casesVO.getMedicalList());
        count+=casesSymptomMapper.deleteBatchIds(casesVO.getSymptomList());
        count+=casesMapper.deleteById(casesVO.getId());
        return count>=3;
    }

    @Transactional
    @Override
    public boolean updateCase(CasesVO casesVO) {
        int count = 0;
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cases_id", casesVO.getId());

        count+=casesMedicalMapper.delete(queryWrapper);
        count+=casesSymptomMapper.delete(queryWrapper);
        count+=casesMapper.updateById(casesVO);
        casesVO.getMedicalList()
                .forEach(m -> casesMedicalMapper.insert(new CasesMedical(null,casesVO.getId(),m)));
        casesVO.getSymptomList()
                .forEach(s -> casesSymptomMapper.insert(new CasesSymptom(null,casesVO.getId(),s)));

        return count>=3;
    }

    @Override
    public Map<String, Integer> getCaseDailyData() {
        Map<String, Integer> data = new HashMap<String, Integer>();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        Integer dayup = casesMapper.getCaseDailyUp();
        queryWrapper.eq("diagnosis","无症状");
        Integer noSymptom = casesMapper.selectCount(queryWrapper);
        queryWrapper.clear();
        queryWrapper.eq("outcome","好转");
        Integer nowCount = casesMapper.selectCount(queryWrapper);
        queryWrapper.clear();
        queryWrapper.eq("outcome","痊愈");
        Integer cureCount = casesMapper.selectCount(queryWrapper);
        queryWrapper.clear();
        Integer total = casesMapper.selectCount(new QueryWrapper());
        data.put("dayUp",dayup);
        data.put("noSymptom",noSymptom);
        data.put("nowCount",nowCount);
        data.put("cureCount",cureCount);
        data.put("total",total);
        return data;
    }

    @Override
    public Map<String, Object> getCaseData() {
        Map<String, Object> data = new HashMap<String, Object>();

        List<CaseData> symtomTop10 = symptomMapper.getSymtomTop10();
        data.put("symtomTop10",symtomTop10);
        List<CaseData> medicalTop10 = medicalMapper.getMedicalTop10();
        data.put("medicalTop10",medicalTop10);
        return data;
    }

    @Override
    public Map<String, Object> getCaseAnalyseData() {
        Map<String, Object> data = new HashMap<String, Object>();
        List<CaseData> monthCount = casesMapper.getCasesMonthCount();
        addMonth(monthCount);
        data.put("monthCount",monthCount);

        List<CaseData> efficacyCount = casesMapper.getEfficacyCount();
        data.put("efficacyCount",efficacyCount);
        return data;
    }

    private void addMonth(List<CaseData> monthCount){
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        for(int i = 0,j=monthCount.size()-1; i <12;i++) {
            String ym = sdf.format(cal.getTime());

            if(cal.get(Calendar.MONTH) == 0) {
                cal.roll(Calendar.YEAR, -1);
            }
            cal.roll(Calendar.MONTH, -1);

            if(ym.equals(monthCount.get(j).getName())) {
                j--;
                if(j<0) break;
                continue;
            }
            monthCount.add(new CaseData(ym,0));
        }
        Collections.sort(monthCount);   // 添加后排序
    }
}
